#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int lenA = 0;
    int lenB = 0;
    struct ListNode* head_a = headA;
    struct ListNode* head_b = headB;
    while (head_a)
    {
        head_a = head_a->next;
        lenA++;
    }
    while (head_b)
    {
        head_b = head_b->next;
        lenB++;
    }
    head_a = headA;
    head_b = headB;
    int i = lenA - lenB;
    if (lenA > lenB)
    {
        while (i--)
        {
            head_a = head_a->next;
        }
        while (head_a)
        {
            if (head_a == head_b)
            {
                return head_a;
            }
            head_a = head_a->next;
            head_b = head_b->next;
        }
        return NULL;
    }
    else
    {
        i = -i;
        while (i--)
        {
            head_b = head_b->next;
        }
        while (head_b)
        {
            if (head_a == head_b)
            {
                return head_a;
            }
            head_a = head_a->next;
            head_b = head_b->next;
        }
        return NULL;
    }
}